﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using NLog;

namespace Aandelendelen.Daily
{
	public class DeleteLogFile : DailyJob
	{
		private static Logger logger = LogManager.GetCurrentClassLogger();

		public DeleteLogFile(XmlNode jobNode)
			: base(jobNode, "Deleting old logfile...")
		{ }

		public override void Execute()
		{
			this.OnStart();

			try
			{
				this.DeleteOldLogFile();
			}
			catch (Exception e)
			{
				this.OnError(e);
			}

			this.OnEnd();

		}

		private void DeleteOldLogFile()
		{
			double days = 14;

			string fileName = System.Web.Hosting.HostingEnvironment.MapPath("/App_Data/") + DateTime.Now.AddDays(-days).ToString("yyyy-MM-dd") + ".log";

			try
			{
				System.IO.File.Delete(fileName);
			}
			catch (Exception e)
			{
				logger.ErrorException("Could not delete " + fileName, e);
			}
		}
	}
}